import java.io.BufferedReader;  
import java.io.InputStreamReader;  
import java.text.ParseException;  
import java.text.SimpleDateFormat;  
import java.util.ArrayList;  
import java.util.Date;  
import java.util.List;  
  
class KB {  
    public static String scan() {  
        String str = "";  
        try {  
            BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));  
            str = buf.readLine();  
        } catch (Exception e) {  
        }  
        return str;  
    }  
}  
  
class Bill {  
    String date;  
    double money;  
    String type;  
    String note;  
  
    Bill(String date, double money, String type, String note) {  
        this.date = date;  
        this.money = money;  
        this.type = type;  
        this.note = note;  
    }  
}  
  
class Cust {  
    List<Bill> bills = new ArrayList<>();  
  
    void Income() {  
        System.out.print("请输入日期(如2024.9.10)：");  
        String date = KB.scan();  
        System.out.print("请输入收入金额（必须为正数）：");  
        double amount = Double.parseDouble(KB.scan());  
        if (amount <= 0) {  
            System.out.println("收入金额必须为正数！");  
            return;  
        }  
        System.out.print("请输入类别：");  
        String type = KB.scan();  
        System.out.print("请输入备注：");  
        String note = KB.scan();  
        bills.add(new Bill(date, amount, type, note));  
    }  
  
    void Expense() {  
        System.out.print("请输入日期(如2024.9.10)：");  
        String date = KB.scan();  
        System.out.print("请输入支出金额（必须为正数）：");  
        double amount = Double.parseDouble(KB.scan());  
        if (amount <= 0) {  
            System.out.println("支出金额必须为正数！");  
            return;  
        }  
        System.out.print("请输入类别（如餐饮、交通、购物等）：");  
        String type = KB.scan();  
        System.out.print("请输入备注：");  
        String note = KB.scan();  
  
        bills.add(new Bill(date, -amount, type, note));  
    }  
  
    void showBills() {  
        System.out.println("收入账单：");  
        for (Bill bill : bills) {  
            if (bill.money > 0) {  
                System.out.println(bill.date + " " + bill.money + " " + bill.type + " " + bill.note);  
            }  
        }  
  
        System.out.println("支出账单：");  
        for (Bill bill : bills) {  
            if (bill.money < 0) {  
                System.out.println(bill.date + " " + (-bill.money) + " " + bill.type + " " + bill.note);  
            }  
        }  
    }  
  
    void queryBillsByDate(String date) throws ParseException {  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.M.dd");  
        Date queryDate = sdf.parse(date);  
  
        System.out.println("收入账单：");  
        for (Bill bill : bills) {  
            Date billDate = sdf.parse(bill.date);  
            if (bill.money > 0 && billDate.equals(queryDate)) {  
                System.out.println(bill.date + " " + bill.money + " " + bill.type + " " + bill.note);  
            }  
        }  
  
        System.out.println("支出账单：");  
        for (Bill bill : bills) {  
            Date billDate = sdf.parse(bill.date);  
            if (bill.money < 0 && billDate.equals(queryDate)) {  
                System.out.println(bill.date + " " + (-bill.money) + " " + bill.type + " " + bill.note);  
            }  
        }  
    }  
  
    void queryBillsByDateRange(String startDate, String endDate) throws ParseException {  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.M.dd");  
        Date start = sdf.parse(startDate);  
        Date end = sdf.parse(endDate);  
  
        System.out.println("收入账单：");  
        for (Bill bill : bills) {  
            Date billDate = sdf.parse(bill.date);  
            if (bill.money > 0 && billDate.after(start) && billDate.before(end)) {  
                System.out.println(bill.date + " " + bill.money + " " + bill.type + " " + bill.note);  
            }  
        }  
  
        System.out.println("支出账单：");  
        for (Bill bill : bills) {  
            Date billDate = sdf.parse(bill.date);  
            if (bill.money < 0 && billDate.after(start) && billDate.before(end)) {  
                System.out.println(bill.date + " " + (-bill.money) + " " + bill.type + " " + bill.note);  
            }  
        }  
    }  
  
    void  queryBillsByType(String type) {  
        System.out.println("收入账单：");  
        for (Bill bill : bills) {  
            if (bill.money > 0 && bill.type.equalsIgnoreCase(type)) {  
                System.out.println(bill.date + " " + bill.money + " " + bill.type + " " + bill.note);  
            }  
        }  
  
        System.out.println("支出账单：");  
        for (Bill bill : bills) {  
            if (bill.money < 0 && bill.type.equalsIgnoreCase(type)) {  
                System.out.println(bill.date + " " + (-bill.money) + " " + bill.type + " " + bill.note);  
            }  
        }  
    }  
}  
  
public class Main {  
    public static void main(String[] args) {  
        Cust c = new Cust();  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.M.dd");  
        while (true) {  
            System.out.println("1.记录收入");  
            System.out.println("2.记录支出");  
            System.out.println("3.展示账单");  
            System.out.println("4.按日期查询账单");  
            System.out.println("5.按日期范围查询账单");  
            System.out.println("6.按类别查询账单");  
            System.out.println("7.退出");  
            System.out.print("请选择你需要的功能:");  
            int choice = Integer.parseInt(KB.scan());  
            switch (choice) {  
                case 1:  
                    c.Income();  
                    break;  
                case 2:  
                    c.Expense();  
                    break;  
                case 3:  
                    c.showBills();  
                    break;  
                case 4:  
                    System.out.print("请输入查询日期(如2024.9.10)：");  
                    try {  
                        String date = KB.scan();  
                        c.queryBillsByDate(date);  
                    } catch (ParseException e) {  
                        System.out.println("日期格式错误，请重新输入！");  
                    }  
                    break;  
                case 5:  
                    System.out.print("请输入查询开始日期(如2024.9.10)：");  
                    String startDate = KB.scan();  
                    System.out.print("请输入查询结束日期(如2024.9.15)：");  
                    String endDate = KB.scan();  
                    try {  
                        c.queryBillsByDateRange(startDate, endDate);  
                    } catch (ParseException e) {  
                        System.out.println("日期格式错误，请重新输入！");  
                    }  
                    break;  
                case 6:  
                    System.out.print("请输入查询类别：");  
                    String type = KB.scan();  
                    c.queryBillsByType(type);  
                    break;  
                case 7:  
                    System.exit(1);  
                default:  
                    System.out.println("输入数字有误,请重新输入！");  
                    break;  
//更改代码

            }  
        }  
    }  
}

